package com.epidemic.project.system.dict.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.epidemic.common.constant.UserConstants;
import com.epidemic.common.exception.BusinessException;
import com.epidemic.common.utils.StringUtils;
import com.epidemic.common.utils.security.ShiroUtils;
import com.epidemic.common.utils.text.Convert;
import com.epidemic.framework.web.domain.Ztree;
import com.epidemic.project.system.dict.domain.DictData;
import com.epidemic.project.system.dict.domain.DictType;
import com.epidemic.project.system.dict.mapper.DictDataMapper;
import com.epidemic.project.system.dict.mapper.DictTypeMapper;
import com.epidemic.project.system.dict.utils.DictUtils;

/**
 * 字典 业务层处理
 *
 * @author sys
 */
@Service
public class DictTypeServiceImpl implements IDictTypeService {
    @Autowired
    private DictTypeMapper dictTypeMapper;

    @Autowired
    private DictDataMapper dictDataMapper;

    /**
     * 项目启动时，初始化字典到缓存
     */
    @PostConstruct
    public void init() {
        /*List<DictType> dictTypeList = dictTypeMapper.selectDictTypeAll();*/
        List<DictType> dictTypeList = new ArrayList<>();

        DictType dictType1 = new DictType("sys_user_sex");
        List<DictData> dictDatas1 = new ArrayList<>();
        dictDatas1.add(new DictData(1L, 1L, "男", "0"));
        dictDatas1.add(new DictData(2L, 2L, "女", "1"));
        dictDatas1.add(new DictData(3L, 3L, "未知", "2"));
        DictUtils.setDictCache("sys_user_sex", dictDatas1);

        DictType dictType2 = new DictType("sys_show_hide");
        List<DictData> dictDatas2 = new ArrayList<>();
        dictDatas2.add(new DictData(4L, 1L, "显示", "0"));
        dictDatas2.add(new DictData(5L, 2L, "隐藏", "1"));
        DictUtils.setDictCache("sys_show_hide", dictDatas2);

        DictType dictType3 = new DictType("sys_normal_disable");
        List<DictData> dictDatas3 = new ArrayList<>();
        dictDatas3.add(new DictData(6L, 1L, "正常", "0"));
        dictDatas3.add(new DictData(7L, 2L, "停用", "1"));
        DictUtils.setDictCache("sys_normal_disable", dictDatas3);

        DictType dictType4 = new DictType("sys_job_status");
        List<DictData> dictDatas4 = new ArrayList<>();
        dictDatas4.add(new DictData(8L, 1L, "正常", "0"));
        dictDatas4.add(new DictData(9L, 2L, "暂停", "1"));
        DictUtils.setDictCache("sys_job_status", dictDatas4);

        DictType dictType5 = new DictType("sys_job_group");
        List<DictData> dictDatas5 = new ArrayList<>();
        dictDatas5.add(new DictData(10L, 1L, "默认", "DEFAULT"));
        dictDatas5.add(new DictData(11L, 2L, "系统", "SYSTEM"));
        DictUtils.setDictCache("sys_job_group", dictDatas5);

        DictType dictType6 = new DictType("sys_yes_no");
        List<DictData> dictDatas6 = new ArrayList<>();
        dictDatas6.add(new DictData(12L, 1L, "是", "Y"));
        dictDatas6.add(new DictData(13L, 2L, "否", "N"));
        DictUtils.setDictCache("sys_yes_no", dictDatas6);

        DictType dictType7 = new DictType("sys_notice_type");
        List<DictData> dictDatas7 = new ArrayList<>();
        dictDatas7.add(new DictData(14L, 1L, "通知", "1"));
        dictDatas7.add(new DictData(15L, 2L, "公告", "2"));
        DictUtils.setDictCache("sys_notice_type", dictDatas7);

        DictType dictType8 = new DictType("sys_notice_status");
        List<DictData> dictDatas8 = new ArrayList<>();
        dictDatas8.add(new DictData(16L, 1L, "正常", "0"));
        dictDatas8.add(new DictData(17L, 2L, "关闭", "1"));
        DictUtils.setDictCache("sys_notice_status", dictDatas8);

        DictType dictType9 = new DictType("sys_oper_type");
        List<DictData> dictDatas9 = new ArrayList<>();
        dictDatas9.add(new DictData(18L, 99L, "其他", "0"));
        dictDatas9.add(new DictData(19L, 1L, "新增", "1"));
        dictDatas9.add(new DictData(20L, 2L, "修改", "2"));
        dictDatas9.add(new DictData(21L, 3L, "删除", "3"));
        dictDatas9.add(new DictData(22L, 4L, "授权", "4"));
        dictDatas9.add(new DictData(23L, 5L, "导出", "5"));
        dictDatas9.add(new DictData(24L, 6L, "导入", "6"));
        dictDatas9.add(new DictData(25L, 7L, "强退", "7"));
        dictDatas9.add(new DictData(26L, 8L, "生成代码", "8"));
        dictDatas9.add(new DictData(27L, 9L, "生成代码", "9"));
        DictUtils.setDictCache("sys_oper_type", dictDatas9);

        DictType dictType10 = new DictType("sys_common_status");
        List<DictData> dictDatas10 = new ArrayList<>();
        dictDatas10.add(new DictData(28L, 1L, "成功", "0"));
        dictDatas10.add(new DictData(29L, 2L, "失败", "1"));
        DictUtils.setDictCache("sys_notice_status", dictDatas10);


        /*for (DictType dictType : dictTypeList) {
            List<DictData> dictDatas = dictDataMapper.selectDictDataByType(dictType.getDictType());
            DictUtils.setDictCache(dictType.getDictType(), dictDatas);
        }*/
    }

    /**
     * 根据条件分页查询字典类型
     *
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public List<DictType> selectDictTypeList(DictType dictType) {
        return dictTypeMapper.selectDictTypeList(dictType);
    }

    /**
     * 根据所有字典类型
     *
     * @return 字典类型集合信息
     */
    @Override
    public List<DictType> selectDictTypeAll() {
        return dictTypeMapper.selectDictTypeAll();
    }

    /**
     * 根据字典类型查询字典数据
     *
     * @param dictType 字典类型
     * @return 字典数据集合信息
     */
    @Override
    public List<DictData> selectDictDataByType(String dictType) {
        List<DictData> dictDatas = DictUtils.getDictCache(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            return dictDatas;
        }
        dictDatas = dictDataMapper.selectDictDataByType(dictType);
        if (StringUtils.isNotEmpty(dictDatas)) {
            DictUtils.setDictCache(dictType, dictDatas);
            return dictDatas;
        }
        return null;
    }

    /**
     * 根据字典类型ID查询信息
     *
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public DictType selectDictTypeById(Long dictId) {
        return dictTypeMapper.selectDictTypeById(dictId);
    }

    /**
     * 根据字典类型查询信息
     *
     * @param dictType 字典类型
     * @return 字典类型
     */
    @Override
    public DictType selectDictTypeByType(String dictType) {
        return dictTypeMapper.selectDictTypeByType(dictType);
    }

    /**
     * 批量删除字典类型
     *
     * @param ids 需要删除的数据
     * @return 结果
     */
    @Override
    public int deleteDictTypeByIds(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            DictType dictType = selectDictTypeById(dictId);
            if (dictDataMapper.countDictDataByType(dictType.getDictType()) > 0) {
                throw new BusinessException(String.format("%1$s已分配,不能删除", dictType.getDictName()));
            }
        }
        int count = dictTypeMapper.deleteDictTypeByIds(dictIds);
        if (count > 0) {
            DictUtils.clearDictCache();
        }
        return count;
    }

    /**
     * 清空缓存数据
     */
    @Override
    public void clearCache() {
        DictUtils.clearDictCache();
    }

    /**
     * 新增保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(DictType dictType) {
        dictType.setCreateBy(ShiroUtils.getLoginName());
        int row = dictTypeMapper.insertDictType(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 修改保存字典类型信息
     *
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(DictType dictType) {
        dictType.setUpdateBy(ShiroUtils.getLoginName());
        DictType oldDict = dictTypeMapper.selectDictTypeById(dictType.getDictId());
        dictDataMapper.updateDictDataType(oldDict.getDictType(), dictType.getDictType());
        int row = dictTypeMapper.updateDictType(dictType);
        if (row > 0) {
            DictUtils.clearDictCache();
        }
        return row;
    }

    /**
     * 校验字典类型称是否唯一
     *
     * @param dict 字典类型
     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(DictType dict) {
        Long dictId = StringUtils.isNull(dict.getDictId()) ? -1L : dict.getDictId();
        DictType dictType = dictTypeMapper.checkDictTypeUnique(dict.getDictType());
        if (StringUtils.isNotNull(dictType) && dictType.getDictId().longValue() != dictId.longValue()) {
            return UserConstants.DICT_TYPE_NOT_UNIQUE;
        }
        return UserConstants.DICT_TYPE_UNIQUE;
    }

    /**
     * 查询字典类型树
     *
     * @param dictType 字典类型
     * @return 所有字典类型
     */
    @Override
    public List<Ztree> selectDictTree(DictType dictType) {
        List<Ztree> ztrees = new ArrayList<Ztree>();
        List<DictType> dictList = dictTypeMapper.selectDictTypeList(dictType);
        for (DictType dict : dictList) {
            if (UserConstants.DICT_NORMAL.equals(dict.getStatus())) {
                Ztree ztree = new Ztree();
                ztree.setId(dict.getDictId());
                ztree.setName(transDictName(dict));
                ztree.setTitle(dict.getDictType());
                ztrees.add(ztree);
            }
        }
        return ztrees;
    }

    public String transDictName(DictType dictType) {
        StringBuffer sb = new StringBuffer();
        sb.append("(" + dictType.getDictName() + ")");
        sb.append("&nbsp;&nbsp;&nbsp;" + dictType.getDictType());
        return sb.toString();
    }
}
